package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.vo.WxFloorGoodsVo;
import com.cskaoyan.market.vo.WxHomeIndexVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// @Service
public class WxHomeServiceImpl_initial implements WxHomeService {

    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketSystemMapper marketSystemMapper;

    @Override
    public WxHomeIndexVo index(MarketUser user) {

        long startTime = System.currentTimeMillis();

        // 先通过配置管理的小程序配置拿到小程序首页的显示数量
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        List<MarketSystem> marketSystemList = marketSystemMapper.selectByExample(marketSystemExample);
        Integer brandCount = null;
        Integer catalogList = null;
        Integer catalogGoods = null;
        Integer hotGoodsCount = null;
        Integer newGoodsCount = null;
        Integer topicCount = null;
        for (final MarketSystem marketSystem : marketSystemList) {
            if ("market_wx_index_brand".equals(marketSystem.getKeyName())) {
                brandCount = Integer.parseInt(marketSystem.getKeyValue());
            } else if ("market_wx_catlog_list".equals(marketSystem.getKeyName())) {
                catalogList = Integer.parseInt(marketSystem.getKeyValue());
            } else if ("market_wx_catlog_goods".equals(marketSystem.getKeyName())) {
                catalogGoods = Integer.parseInt(marketSystem.getKeyValue());
            } else if ("market_wx_index_hot".equals(marketSystem.getKeyName())) {
                hotGoodsCount = Integer.parseInt(marketSystem.getKeyValue());
            } else if ("market_wx_index_new".equals(marketSystem.getKeyName())) {
                newGoodsCount = Integer.parseInt(marketSystem.getKeyValue());
            } else if ("market_wx_index_topic".equals(marketSystem.getKeyName())) {
                topicCount = Integer.parseInt(marketSystem.getKeyValue());
            }
        }
        // banner
        MarketAdExample marketAdExample = new MarketAdExample();
        List<MarketAd> banner = marketAdMapper.selectByExample(marketAdExample);

        // brandList
        MarketBrandExample marketBrandExample = new MarketBrandExample();
        List<MarketBrand> brandList = marketBrandMapper.selectByExample(marketBrandExample);
        if (brandCount != null && brandList.size() > brandCount) {
            brandList = brandList.subList(0, brandCount);
        }

        // channel
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        List<MarketCategory> channel = marketCategoryMapper.selectByExample(marketCategoryExample);
        if (channel.size() > 12) {
            channel = channel.subList(0, 12);
        }

        // couponList
        MarketCouponExample marketCouponExample = new MarketCouponExample();
        List<MarketCoupon> couponList = marketCouponMapper.selectByExample(marketCouponExample);
        if (couponList.size() > 8) {
            couponList = couponList.subList(0, 8);
        }

        // floorGoodsList
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(new MarketCategoryExample());
        Map<Integer, List<MarketCategory>> data = groupByPid(marketCategories);
        List<WxFloorGoodsVo> floorGoodsList = new ArrayList<>();
        List<MarketCategory> categoryListL1 = data.get(0);
        // 设置L1分类展示数量
        if (catalogList != null && categoryListL1.size() > catalogList) {
            categoryListL1 = categoryListL1.subList(0, catalogList);
        }
        for (final MarketCategory categoryL1 : categoryListL1) {
            Integer id = categoryL1.getId();
            String name = categoryL1.getName();
            List<MarketGoods> marketGoodsList = new ArrayList<>();
            List<MarketCategory> categoryListL2 = data.get(categoryL1.getId());
            for (final MarketCategory categoryL2 : categoryListL2) {
                Integer l2Id = categoryL2.getId();
                List<MarketGoods> marketGoods = marketGoodsMapper.selectByCategoryId(l2Id);
                marketGoodsList.addAll(marketGoods);
            }
            // 设置每个L1分类展示商品数量
            if (catalogGoods != null && marketGoodsList.size() > catalogGoods) {
                marketGoodsList = marketGoodsList.subList(0, catalogGoods);
            }
            WxFloorGoodsVo wxFloorGoodsVo = new WxFloorGoodsVo(id, name, marketGoodsList);
            floorGoodsList.add(wxFloorGoodsVo);
        }

        // hotGoodsList
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = marketGoodsExample.createCriteria();
        criteria.andIsHotEqualTo(true);
        List<MarketGoods> hotGoodsList = marketGoodsMapper.selectByExample(marketGoodsExample);
        if (hotGoodsCount != null && hotGoodsList.size() > hotGoodsCount) {
            hotGoodsList = hotGoodsList.subList(0, hotGoodsCount);
        }

        // newGoodsList
        MarketGoodsExample marketGoodsExample2 = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria2 = marketGoodsExample2.createCriteria();
        criteria2.andIsNewEqualTo(true);
        List<MarketGoods> newGoodsList = marketGoodsMapper.selectByExample(marketGoodsExample2);
        if (newGoodsCount != null && newGoodsList.size() > newGoodsCount) {
            newGoodsList = newGoodsList.subList(0, newGoodsCount);
        }

        // topicList
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        List<MarketTopic> topicList = marketTopicMapper.selectByExample(marketTopicExample);
        if (topicCount != null && topicList.size() > topicCount) {
            topicList = topicList.subList(0, topicCount);
        }

        WxHomeIndexVo wxHomeIndexVo = new WxHomeIndexVo(banner, brandList, channel, couponList, floorGoodsList, hotGoodsList, newGoodsList, topicList);

        long endTime = System.currentTimeMillis();
        System.out.println("Initial Cost time: " + (endTime - startTime) + "ms");

        return wxHomeIndexVo;
    }

    private Map<Integer, List<MarketCategory>> groupByPid(List<MarketCategory> marketCategories) {

        Map<Integer, List<MarketCategory>> map = new HashMap<>();
        for (MarketCategory category : marketCategories) {
            Integer pid = category.getPid();
            List<MarketCategory> list = map.get(pid);
            if (list == null) {
                list = new ArrayList<>();
            }
            list.add(category);
            map.put(pid, list);
        }
        return map;
    }
}
